// Time:  O(n)
// Space: O(1)

#include <vector>

using namespace std;

class Solution {
public:
    int longestMountain(vector<int>& arr) {
        if(arr.size() < 3) {
            return 0;
        }
        int result = 0, up_len = 0, down_len = 0;
        for (int i = 1; i < arr.size(); ++i) {
            // a new increase start after a decrease,like 1,2,1,4,5
            if ((down_len && arr[i - 1] < arr[i]) || arr[i - 1] == arr[i]) {
                up_len = down_len = 0;
            }
            if(arr[i - 1] < arr[i]) {
                up_len++;
            }
            if(arr[i - 1] > arr[i]) {
                down_len++;
            }
            if (up_len && down_len) {
                result= max(result, up_len + down_len + 1);
            }
        }
        return result;
    }
};
